package net.skyaims.aiolia.chat.http;

import javax.servlet.http.HttpServlet;

import net.skyaims.aiolia.chat.core.Platform;

import org.eclipse.jetty.server.Connector;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.bio.SocketConnector;
import org.eclipse.jetty.server.handler.ContextHandlerCollection;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder;

import com.sun.jersey.api.core.ClassNamesResourceConfig;
import com.sun.jersey.spi.container.servlet.ServletContainer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Hypertext Transfer Protocol
 *
 * @author wujianing
 */

public abstract class HttpServer extends Platform<HttpOption, HttpEnvironment> {

    private static final Logger log = LoggerFactory.getLogger(HttpServer.class);

    private static final String[] httpArgs = new String[]{"http_instance", "http.conf.properties"};

    public HttpServer() {
        this(httpArgs[0], httpArgs[1]);
    }

    public HttpServer(String instanceName, String optionFile) {
        super(instanceName, optionFile);
        // TODO Auto-generated constructor stub
    }

    @Override
    public void service(HttpOption opt, HttpEnvironment env) throws Exception {
        // TODO Auto-generated method stub
        // 配置服务器环境
        // ServletHolder holder = new ServletHolder(ServletContainer.class);
        // holder.setInitParameter("com.sun.jersey.config.property.resourceConfigClass",
        // "com.sun.jersey.api.core.PackagesResourceConfig");
        // holder.setInitParameter("com.sun.jersey.config.property.packages",
        // "net.skyaims.test");

        Connector connector = new SocketConnector();
        connector.setPort(opt.getPort());

        ClassNamesResourceConfig resourceConfig = new ClassNamesResourceConfig(env.getResources());
        HttpServlet apiServlet = new ServletContainer(resourceConfig);
        ServletContextHandler apiHandler = new ServletContextHandler();
        apiHandler.setContextPath("/");
        apiHandler.addServlet(new ServletHolder(apiServlet), "/*");
        // apiHandler.setResourceBase(System.getProperty("user.dir"));

        ContextHandlerCollection handlers = new ContextHandlerCollection();
        handlers.addHandler(apiHandler);

        // 启动服务器
        Server server = new Server();
        server.setConnectors(new Connector[]{connector});
        server.setHandler(handlers);
        server.start();

    }

    @Override
    protected HttpOption loadOpt(String optionFile) {
        // TODO Auto-generated method stub
        log.info("Loading Option File : " + optionFile);
        HttpOption option = new HttpOption();
        option.setPath(optionFile);
        return option;
    }

    @Override
    protected HttpEnvironment loadEnv(String instanceName) {
        // TODO Auto-generated method stub
        log.info("Loading Environment Component");
        HttpEnvironment httpEnvironment = new HttpEnvironment();
        httpEnvironment.setInstanceName(instanceName);
        return httpEnvironment;
    }

}